<?php
namespace frontend\controllers;

use dosamigos\qrcode\lib\Enum;
use dosamigos\qrcode\QrCode;
use Yii;
use common\models\LoginForm;
use backend\models\SignupForm;

/**
 * Site controller
 */
class SiteController extends BaseController
{

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionGetImage()
    {
        return $this->asJson([
            'code' => 200,
            'msg' => 'ok',
            'data' => [
                'url' => Yii::$app->request->hostInfo . '/site/captcha'
            ]
        ]);
    }

    public function actionQrcode()
    {
        header('Content-Type:image/png');
        QrCode::png('xxxwfsdf3wekdfsh',false,Enum::QR_ECLEVEL_H,8);die;
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Logs in a user.
     *
     * @return mixed
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        $model->load(Yii::$app->request->post());
        $r = $model->login();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logs out the current user.
     *
     * @return mixed
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays about page.
     *
     * @return mixed
     */
    public function actionAbout()
    {
        $password = '123456';
        $re=hash('sha256', $password, true);
        $password = bin2hex($re);
        $password = base64_encode($password);
        return md5($password);
    }

    /**
     * Displays about page.
     *
     * @return mixed
     */
    public function actionApi()
    {
        return $this->render('api');
    }

    /**
     * Signs user up.
     *
     * @return mixed
     */
    public function actionSignup()
    {
        $model = new SignupForm();
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                if (Yii::$app->getUser()->login($user)) {
                    return $this->goHome();
                }
            }
        }

        return $this->render('signup', [
            'model' => $model,
        ]);
    }

}
